package com.example.ytyproject.component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.ytyproject.entity.Logs;
import com.example.ytyproject.service.ILogsService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * es工具类
 */

@Component
@Slf4j
public class ElasticsearchService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private ILogsService logsService;

    private static final String indices = "indices";

    /**
     * 判断索引是否存在
     * index:索引id
     */
    public boolean checkIndex(String index) {
        try {
            return restHighLevelClient.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Boolean.FALSE ;
    }

    /**
     * 添加索引
     *
     * @param indices 索引id
     * @throws IOException
     */
    public void createIndex(String indices) throws IOException {
        // 定义索引名称
        CreateIndexRequest request = new CreateIndexRequest(indices);
        // 发送请求到ES
        CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        // 处理响应结果
        System.out.println("添加索引是否成功：" + response.isAcknowledged());
    }

    /**
     * 删除索引
     */
    public void deleteIndex(String indices)throws IOException {
        // 定义索引名称
        DeleteIndexRequest request = new DeleteIndexRequest(indices);
        // 发送请求到ES
        AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        // 处理响应结果
        System.out.println("删除索引是否成功：" + response.isAcknowledged());
    }

    /**
     * 添加数据到es
     * @param logs
     */
    public void insertToEs(Logs logs) {
        try {
            if (!checkIndex(indices)) {
                createIndex(indices);
            }
            BulkRequest bulkRequest = new BulkRequest();
            IndexRequest request = new IndexRequest(indices);
            JSONObject json = (JSONObject) JSONObject.toJSON(logs);
            request.source(json);
            request.id(json.getString("id"));
            bulkRequest.add(request);
            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
            System.out.println("------------------------------------数据添加成功------------------------------------");
        } catch (IOException ioException) {
            log.error("添加索引{}异常:{}", "indices",ioException.getMessage(), ioException);
        }
    }

    /**
     * 添加数据到es
     */
    public void batchInsertToEs() {
        try {
            List<Logs> list = logsService.list();
            //利用bulk进行导入
            BulkRequest bulkRequest = new BulkRequest();
            //循环goodsList，创建IndexRequest添加数据
            for (Logs logs:list){
                IndexRequest indexRequest = new IndexRequest(indices);
                indexRequest.id(logs.getId()).source(JSON.toJSONString(logs), XContentType.JSON);
                bulkRequest.add(indexRequest);
            }
            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
            System.out.println("------------------------------------数据添加成功------------------------------------");
        } catch (IOException ioException) {
            log.error("添加索引{}异常:{}", "indices",ioException.getMessage(), ioException);
        }
    }

    /**
     * 更新es数据
     * @param logs
     */
    public void updateEsData(Logs logs) {
        try {
            String data = JSONObject.toJSONString(logs);
            JSONObject json = (JSONObject) JSONObject.toJSON(logs);
            if (StringUtils.isEmpty(json.getString("id"))){
                throw new RuntimeException("id为空！");
            }
            UpdateRequest updateRequest = new UpdateRequest(indices, json.getString("id")).doc(data, XContentType.JSON);
            updateRequest.docAsUpsert(true);
            UpdateResponse response = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);

            String id = response.getId();

        } catch (IOException ioException) {
            log.error("添加索引{}异常:{}", indices,ioException.getMessage(), ioException);
        }
    }

    /**
     * 查询全部数据
     */
    public List<Map<String,Object>> list() {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 组装条件
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
//        queryBuilder.must(QueryBuilders.termQuery("createTime", 1611378102795L));
//        queryBuilder.mustNot(QueryBuilders.termQuery("name","北京-李四"));
        sourceBuilder.query(queryBuilder);
        SearchRequest searchRequest = new SearchRequest(indices);
        searchRequest.source(sourceBuilder);
        try {
            SearchResponse searchResp = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            List<Map<String,Object>> data = new ArrayList<>() ;
            SearchHit[] searchHitArr = searchResp.getHits().getHits();
            for (SearchHit searchHit:searchHitArr){
                Map<String,Object> temp = searchHit.getSourceAsMap();
                temp.put("id",searchHit.getId()) ;
                data.add(temp);
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null ;
    }

    public long queryTotal() {
        // 指定创建时间
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        // 按照条件查询
//        queryBuilder.must(QueryBuilders.termQuery("createTime", 1611378102795L));
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(queryBuilder);
        CountRequest countRequest = new CountRequest(indices);
        countRequest.source(sourceBuilder);
        try {
            CountResponse countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
            return countResponse.getCount();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
